﻿using System;
using System.Data;
using System.Data.SqlClient;
using vtech_frameWorks.DAO;
using vtech_frameWorks.Exceptions;

namespace HospitalManagement.Clinics
{
    public class ClsClinicsBS
    {
        #region GetAllClinics
        public static DataTable GetAllClinics()
        {
            using (DsClinics dsClinics = new DsClinics())
            {
                dsClinics.DtClinics.Merge(ClsClinicsDAO.GetAll());
                return dsClinics.DtClinics;
            }
        }
        #endregion

        #region GetAllClinicsInActive
        /// <summary>
        /// Lấy tất cả các phòng ban còn hoạt động
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllClinicsInActive()
        {
            using (DsClinics dsClinics = new DsClinics())
            {
                DataRow[] drow = ClsClinicsDAO.GetAll().Select("Inactive=0");
                foreach (DataRow row in drow)
                    dsClinics.DtClinics.ImportRow(row);
                return dsClinics.DtClinics;
            }
        }
        #endregion

        #region "Insert"
        public static void DoInsert(DataTable dt)
        {
            SqlConnection conn = null;
            SqlTransaction tran = null;

            try
            {
                conn = ClsBaseDbConnectionDAO.GetConnection();
                conn.Open();
                tran = conn.BeginTransaction();

                foreach (DataRow drow in dt.Rows)
                {
                    ClsClinicsDAO.Insert(drow);
                }

                tran.Commit();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ClsInformation.Information(ex.Message);
                }
                if (tran != null)
                {
                    tran.Rollback();
                }
            }
            finally
            {
                if (conn != null && conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region "Edit"
        public static void DoEdit(DataTable dt)
        {
            SqlConnection conn = null;
            SqlTransaction tran = null;

            try
            {
                conn = ClsBaseDbConnectionDAO.GetConnection();
                conn.Open();
                tran = conn.BeginTransaction();

                foreach (DataRow drow in dt.Rows)
                {
                    ClsClinicsDAO.Edit(drow);
                }

                tran.Commit();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ClsInformation.Information(ex.Message);
                }
                if (tran != null)
                {
                    tran.Rollback();
                }
            }
            finally
            {
                if (conn != null && conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }
        #endregion

        #region "Delete"
        public static void DoDelete(DataRow drow)
        {
            SqlConnection conn = null;
            SqlTransaction tran = null;

            try
            {
                conn = ClsBaseDbConnectionDAO.GetConnection();
                conn.Open();
                tran = conn.BeginTransaction();
                ClsClinicsDAO.Delete(drow);
                tran.Commit();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ClsInformation.Information(ex.Message);
                }
                if (tran != null)
                {
                    tran.Rollback();
                }
            }
            finally
            {
                if (conn != null && conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }
        #endregion
    }
}
